<?php

namespace app\models;

use think\Model;

/**
 * Class TeacherModel.
 */
class TeacherModel extends Model
{
    protected $table = 'edu_teacher';

    /**
     * @param $campusId
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function getByCampusId($campusId)
    {
        if (!$campusId) {
            return [];
        }

        return $this->where(['campus_id' => $campusId,'status'=>1])
            ->field('id,teacher_name')
            ->select();
    }

	/**
	 * modify by Small Y
	 *
	 * @param $data
	 * @return array|false|\PDOStatement|string|\think\Collection
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
    public function getByDepartmentId($data)
    {
        if (!$data) {
            return [];
        }

        return $this->where(['department_id' => $data['department_id'],'campus_id'=>$data['campus_id'],'status'=>1])
            ->field('id,teacher_name')
            ->select();
    }

	/**
	 * modify by Small Y
	 *
	 * @param $data
	 * @return array|false|\PDOStatement|string|\think\Collection
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
    public function getByGradeId($data)
    {
        if (!$data) {
            return [];
        }

        return $this->where(['grade_id' => $data['grade_id'],'campus_id'=>$data['campus_id'],'status'=>1])
            ->field('id,teacher_name')
            ->select();
    }

	/**
	 * modify by Small Y
	 *
	 * @param $data
	 * @return array|false|\PDOStatement|string|\think\Collection
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
    public function getBySubjectId($data)
    {
        if (!$data) {
            return [];
        }

        return $this->where(['subject_id' => $data['subject_id'], 'campus_id'=>$data['campus_id'],'status'=>1])
            ->field('id,teacher_name')
            ->select();
    }

    /**
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getTeachertLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;

        $CourseModel = new CourseModel(); //课程
        $GradesDetailsModel = new GradesDetailsModel();  // 班级
        $StudentSubjectInfosClassModel = new StudentSubjectInfosClassModel();  //详情


        $where = [];
        if (isset($param['teacher_name']) && '' !== $param['teacher_name']) {
            $where['teacher_name'] = ['like', '%'.$param['teacher_name'].'%'];
        }
        if (isset($param['status']) && '' !== $param['status']) {
            $where['status'] = $param['status'];
        }

        if (isset($param['select_campus']) && '' !== $param['select_campus']) {   //选择校区
            $where['campus_id'] = $param['select_campus'];
        }else {
            if (get_current_type() == 4 || get_current_type() == 5) {
                $where['campus_id'] = get_current_campus_id();
            } elseif (get_current_type() == 2 || get_current_type() == 3) {
                $where['campus_id'] = ['in', get_current_campus_id()];
            }
        }


        if(get_current_type() == 5){
            if(get_current_group_type() == 2){  //老师类
                $where['id'] = get_current_id();
            }else if(get_current_group_type() == 3){  //助教类
                $where['id'] = get_current_id();
            }
        }

        //1v1
        $course_id_1v1_arr = $CourseModel->where(['course_type' => 41])->column('id');

        //1vN
        $course_id_1vn_arr = $CourseModel->where(['course_type' => 43])->column('id');

        //班课
        $course_id_class_arr = $CourseModel->where(['course_type' => 42])->column('id');

        $where_elimination_1v1 = [];
        $where_elimination_1v1['status'] = 1;
        $where_elimination_1v1['give_status'] = 1;


        $where_elimination_1v1_arrears = [];
        $where_elimination_1v1_arrears['status'] = 1;
        $where_elimination_1v1_arrears['give_status'] = 1;
        $where_elimination_1v1_arrears['arrears_status'] = 1;


        $where_elimination_1vn = [];
        $where_elimination_1vn['status'] = 1;
        $where_elimination_1vn['give_status'] = 1;

        $where_elimination_1vn_arrears = [];
        $where_elimination_1vn_arrears['status'] = 1;
        $where_elimination_1vn_arrears['give_status'] = 1;
        $where_elimination_1vn_arrears['arrears_status'] = 1;


        $where_elimination_class = [];
        $where_elimination_class['status'] = 1;
        $where_elimination_class['give_status'] = 1;


        $where_elimination_class_arrears = [];
        $where_elimination_class_arrears['status'] = 1;
        $where_elimination_class_arrears['give_status'] = 1;
        $where_elimination_class_arrears['arrears_status'] = 1;

        //时间段
        if (isset($param['start_date_elimination']) && '' !== $param['start_date_elimination'] && isset($param['end_date_elimination']) && '' !== $param['end_date_elimination']) {
            $where_elimination_1v1['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
            $where_elimination_1v1_arrears['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
            $where_elimination_1vn['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
            $where_elimination_1vn_arrears['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
            $where_elimination_class['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
            $where_elimination_class_arrears['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
        }
        $listData = self::where($where)->order('update_time desc')
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();

        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $item['status_text'] = (1 === $item['status']) ? '启用中' : '禁用中';
                $item['campus_id'] = CampusModel::get(['id' => $item['campus_id']])['campus_name'];
                $item['grades_nums']  = $GradesDetailsModel->where(['teacher_id' => $item['id']])->count();
                $grades_arr = $GradesDetailsModel->where(['teacher_id' => $item['id']])->column('id');
                $item['grades_info_class_nums'] = $StudentSubjectInfosClassModel->where(['class_subject_id' => ['in', implode(',',$grades_arr)], 'status' => 1])->count();
                $item['grades_class_length'] = $StudentSubjectInfosClassModel->where(['class_subject_id' => ['in', implode(',',$grades_arr)], 'status' => 1])->sum('duration');
                $item['grades_class_length'] = ($item['grades_class_length']/60).'(小时)';

                //1V1总课消
                $grades_id_1v1_arr = $GradesDetailsModel->where(['course_id' => ['in', implode(',',$course_id_1v1_arr)], 'teacher_id' => $item['id']])->column('id');
                $grades_id_1v1_arr_str = implode(',',$grades_id_1v1_arr);

                $where_elimination_1v1['class_subject_id'] = ['in', $grades_id_1v1_arr_str];
                $res_total_1v1_amount = $StudentSubjectInfosClassModel->where($where_elimination_1v1)->sum('amount_deducted');  //费用
                $res_total_1v1_amount = round(abs($res_total_1v1_amount / 100),2);

                $res_total_1v1_nums = $StudentSubjectInfosClassModel->where($where_elimination_1v1)->count();  //次数


                $res_total_1v1_duration = $StudentSubjectInfosClassModel->where($where_elimination_1v1)->sum('duration');  //时长
                $res_total_1v1_duration = $res_total_1v1_duration / 60; //小时

                $item['class_elimination_one'] = $res_total_1v1_amount.'(元)/'.$res_total_1v1_nums.'(人次)'.'/'.$res_total_1v1_duration.'(小时)';


                //1V1欠费课消
                $where_elimination_1v1_arrears['class_subject_id'] = ['in', $grades_id_1v1_arr_str];
                $res_arrears_total_1v1_amount = $StudentSubjectInfosClassModel->where($where_elimination_1v1_arrears)->sum('arrears_amount');  //费用
                $res_arrears_total_1v1_amount = round(abs($res_arrears_total_1v1_amount / 100),2);

                $res_arrears_total_1v1_nums = $StudentSubjectInfosClassModel->where($where_elimination_1v1_arrears)->count();  //次数


                $res_arrears_total_1v1_duration = $StudentSubjectInfosClassModel->where($where_elimination_1v1_arrears)->sum('duration');  //时长
                $res_arrears_total_1v1_duration = $res_arrears_total_1v1_duration / 60; //小时

                $item['class_elimination_one_arrears'] = $res_arrears_total_1v1_amount.'(元)/'.$res_arrears_total_1v1_nums.'(人次)'.'/'.$res_arrears_total_1v1_duration.'(小时)';


                //1VN总课消
                $grades_id_1vn_arr = $GradesDetailsModel->where(['course_id' => ['in', implode(',',$course_id_1vn_arr)], 'teacher_id' => $item['id']])->column('id');
                $grades_id_1vn_arr_str = implode(',',$grades_id_1vn_arr);

                $where_elimination_1vn['class_subject_id'] = ['in', $grades_id_1vn_arr_str];
                $res_total_1vn_amount = $StudentSubjectInfosClassModel->where($where_elimination_1vn)->sum('amount_deducted');  //费用
                $res_total_1vn_amount = round(abs($res_total_1vn_amount / 100),2);

                $res_total_1vn_nums = $StudentSubjectInfosClassModel->where($where_elimination_1vn)->count();  //次数


                $res_total_1vn_duration = $StudentSubjectInfosClassModel->where($where_elimination_1vn)->sum('duration');  //时长
                $res_total_1vn_duration = $res_total_1vn_duration / 60; //小时

                $item['class_elimination_n'] = $res_total_1vn_amount.'(元)/'.$res_total_1vn_nums.'(人次)'.'/'.$res_total_1vn_duration.'(小时)';


                $where_elimination_1vn['class_subject_id'] = ['in', $grades_id_1vn_arr_str];
                $res_arrears_total_1vn_amount = $StudentSubjectInfosClassModel->where($where_elimination_1vn_arrears)->sum('arrears_amount');  //费用
                $res_arrears_total_1vn_amount = round(abs($res_arrears_total_1vn_amount / 100),2);

                $res_arrears_total_1vn_nums = $StudentSubjectInfosClassModel->where($where_elimination_1vn_arrears)->count();  //次数


                $res_arrears_total_1vn_duration = $StudentSubjectInfosClassModel->where($where_elimination_1vn_arrears)->sum('duration');  //时长
                $res_arrears_total_1vn_duration = $res_arrears_total_1vn_duration / 60; //小时

                $item['class_elimination_n_arrears'] = $res_arrears_total_1vn_amount.'(元)/'.$res_arrears_total_1vn_nums.'(人次)'.'/'.$res_arrears_total_1vn_duration.'(小时)';



                //班课总课消
                $grades_id_class_arr = $GradesDetailsModel->where(['course_id' => ['in', implode(',',$course_id_class_arr)], 'teacher_id' => $item['id']])->column('id');
                $grades_id_class_arr_str = implode(',',$grades_id_class_arr);

                $where_elimination_class['class_subject_id'] = ['in', $grades_id_class_arr_str];
                $res_total_class_amount = $StudentSubjectInfosClassModel->where($where_elimination_class)->sum('amount_deducted');  //费用
                $res_total_class_amount = round(abs($res_total_class_amount / 100),2);

                $res_total_class_nums = $StudentSubjectInfosClassModel->where($where_elimination_class)->count();  //次数


                $res_total_class_duration = $StudentSubjectInfosClassModel->where($where_elimination_class)->sum('duration');  //时长
                $res_total_class_duration = $res_total_class_duration / 60; //小时

                $item['class_elimination'] = $res_total_class_amount.'(元)/'.$res_total_class_nums.'(人次)'.'/'.$res_total_class_duration.'(小时)';


                //班课欠费课消
                $where_elimination_class['class_subject_id'] = ['in', $grades_id_class_arr_str];
                $res_arrears_total_class_amount = $StudentSubjectInfosClassModel->where($where_elimination_class_arrears)->sum('arrears_amount');  //费用
                $res_arrears_total_class_amount = round(abs($res_arrears_total_class_amount / 100),2);

                $res_arrears_total_class_nums = $StudentSubjectInfosClassModel->where($where_elimination_class_arrears)->count();  //次数


                $res_arrears_total_class_duration = $StudentSubjectInfosClassModel->where($where_elimination_class_arrears)->sum('duration');  //时长
                $res_arrears_total_class_duration = $res_arrears_total_class_duration / 60; //小时

                $item['class_elimination_arrears'] = $res_arrears_total_class_amount.'(元)/'.$res_arrears_total_class_nums.'(人次)'.'/'.$res_arrears_total_class_duration.'(小时)';

                //总课消
                $class_elimination_total = $res_total_1v1_amount + $res_total_1vn_amount + $res_total_class_amount;
                $class_elimination_nums_total = $res_total_1v1_nums + $res_total_1vn_nums + $res_total_class_nums;
                $class_elimination_duration_total = $res_total_1v1_duration + $res_total_1vn_duration + $res_total_class_duration;
                $item['class_elimination_total'] = $class_elimination_total.'(元)/'.$class_elimination_nums_total.'(人次)'.'/'.$class_elimination_duration_total.'(小时)';

                //总欠费课消
                $class_elimination_total_arrears = $res_arrears_total_1v1_amount + $res_arrears_total_1vn_amount + $res_arrears_total_class_amount;
                $class_elimination_nums_total_arrears = $res_arrears_total_1v1_nums + $res_arrears_total_1vn_nums + $res_arrears_total_class_nums;
                $class_elimination_duration_total_arrears = $res_arrears_total_1v1_duration + $res_arrears_total_1vn_duration + $res_arrears_total_class_duration;
                $item['class_elimination_total_arrears'] = $class_elimination_total_arrears.'(元)/'.$class_elimination_nums_total_arrears.'(人次)'.'/'.$class_elimination_duration_total_arrears.'(小时)';


            }
        }

        return $listData;
    }
}
